import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;



import javax.swing.BoxLayout;
import javax.swing.JPanel;


public class LoginScreen {
        static JFrame frame;
        static JButton submit;
        static JButton cancel; 
        static JTextField username, IPaddress;
        ChatPanel chatpanel;
        
        public LoginScreen() {
                frame = new JFrame("Login");
                frame.setSize(250, 155);
        		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
                submit = new JButton("Submit");
                submit.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                submit();
                        }
                });
                frame.getRootPane().setDefaultButton(submit);
                
                cancel = new JButton("Cancel");
                cancel.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(ActionEvent e) { 
                                System.exit(1);
                        }
                });
                
                username = new JTextField();
                username.setBorder(BorderFactory.createTitledBorder("Username"));
                
                IPaddress = new JTextField();
                IPaddress.setBorder(BorderFactory.createTitledBorder("IP Address (Optional)"));

                JPanel pane = new JPanel();
                pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
                pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
                pane.add(username);
                pane.add(IPaddress);
                frame.add(pane, BorderLayout.NORTH );
                
                JPanel panel = new JPanel();
                panel.add(submit);
                panel.add(cancel);
                
                frame.add(panel, BorderLayout.SOUTH);
                
                frame.setResizable(false);
                frame.setVisible(true);
                
        }

	
	private void submit() {
		if(!username.getText().equals("")) { 
			frame.setVisible(false);
			chatpanel = new ChatPanel(username.getText());
			chatpanel.addUser(username.getText());
			String ip = IPaddress.getText();
			Server s = new Server(chatpanel);
			if(ip.length() > 0)
				s.connectTo(ip);
		}
	}
	
	public static void main(String[] args) { 
		new LoginScreen();
	}
	
}
